package com.um.jdy.platform.screen.manager;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.um.jdy.common.charge.service.OrderService;
import com.um.jdy.platform.screen.entity.vo.*;
import com.um.jdy.platform.station.entity.vo.StationProvinceItemVO;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
 * 大屏图表管理
 *
 * @author 邓南辉
 * 日期 2023/9/14
 * 版权所有 广州优匠科技有限公司
 */
@Component
@Slf4j
public class ScreenManager extends BaseManager<OrderService> {

    /**
     * 大屏数据统计
     *
     * @return
     */
    public ScreenDataVO getScreenData() {
        String comId = service.getComId();
        ScreenDataVO screenDataVO = new ScreenDataVO();

        Date nowTime = new Date();

        DateTime begin = DateUtil.offsetDay(nowTime, -6);
        String beginTime = DateUtil.format(begin, "yyyy-MM-dd") + " 00:00:00";
        String endTime = DateUtil.format(nowTime, "yyyy-MM-dd") + " 23:59:59";

        //用户统计-最近7天内新增用户
        List<ScreenDataItemVO> userCountList = service.getViewModelList("get_user_count_list", ScreenDataItemVO.class, new Object[]{comId, beginTime, endTime});

        //最近7天内订单统计,包括消费统计和订单数统计
        List<ScreenDataItemVO> orderCountList = service.getViewModelList("get_order_count_list", ScreenDataItemVO.class, new Object[]{comId, beginTime, endTime});

        //最近7天内充值统计
        List<ScreenDataItemVO> rechargeCountList = service.getViewModelList("get_recharge_count_list", ScreenDataItemVO.class, new Object[]{comId, beginTime, endTime});

        this.fillList(userCountList, orderCountList, rechargeCountList, nowTime);

        //设备路数统计
        //List<ScreenGunCountVO> gunCountList = service.getViewModelList("get_gun_count_list", ScreenGunCountVO.class, new Object[]{comId, comId});

        //设备类型统计
        List<ScreenDeviceTypeVO> deviceTypeList = service.getViewModelList("get_device_type_list",ScreenDeviceTypeVO.class,new Object[]{comId});

        //充值通知
        List<ScreenRechargeNoticeVO> rechargeNoticeList = service.getViewModelList("get_recharge_notice_list", ScreenRechargeNoticeVO.class, new Object[]{comId, comId});

        //消费通知
        List<ScreenOrderNoticeVO> orderNoticeList = service.getViewModelList("get_order_notice_list", ScreenOrderNoticeVO.class, new Object[]{comId, comId, comId});

        //离线通知
        List<ScreenOfflineNoticeVO> offlineNoticeList = service.getViewModelList("get_offline_notice_list", ScreenOfflineNoticeVO.class, new Object[]{comId, comId});

        //网点订单量排名
        List<ScreenRankItemVO> stationOrderRankList = service.getViewModelList("get_station_order_rank", ScreenRankItemVO.class, new Object[]{comId, comId});

        //地区用户量排名
        List<ScreenRankItemVO> areaUserRankList = service.getViewModelList("get_area_user_rank", ScreenRankItemVO.class, new Object[]{comId});

        //总充值(元)
        Long totalRecharge = service.getCountBySqlName("count_total_recharge", new String[]{comId});

        //总用户(人)
        Long totalUser = service.getCountBySqlName("count_total_user", new String[]{comId});

        //总消费(元)
        Long totalConsume = service.getCountBySqlName("count_total_consume", new String[]{comId});

        //总消费订单(条)
        Long totalOrders = service.getCountBySqlName("count_total_orders", new String[]{comId});


        //设备总数
        Integer totalPileNum = service.getCountBySqlName("get_device_total_count", new Object[]{comId}).intValue();
        //在线设备数
        ScreenDeviceDataVO deviceDataVO = service.getViewModel("get_device_status_count",ScreenDeviceDataVO.class, new Object[]{comId});

        //网点总数
        Integer totalStationNum = service.getCountBySqlName("get_station_overview_total_count", new Object[]{comId}).intValue();

        //运营商总数
        Integer totalMerchantNum = service.getCountBySqlName("get_merchant_total_count", new Object[]{comId}).intValue();

        //地图概览
        List<StationProvinceItemVO> stationProvinceList = service.getViewModelList("get_station_overview_province_list",StationProvinceItemVO.class,new Object[]{comId});

        //设备数量统计
        deviceDataVO.setTotalNum(totalPileNum);
        deviceDataVO.setOnlineRate(totalPileNum > 0 && deviceDataVO.getOnlineNum() > 0 ? (Float.valueOf(String.format("%.2f", deviceDataVO.getOnlineNum() * 100.0 / totalPileNum)) + "%") : "0%");
        deviceDataVO.setOfflineRate(totalPileNum > 0 && deviceDataVO.getOfflineNum() > 0 ? (Float.valueOf(String.format("%.2f", deviceDataVO.getOfflineNum() * 100.0 / totalPileNum)) + "%") : "0%");
        deviceDataVO.setInactiveRate(totalPileNum > 0 && deviceDataVO.getInactiveNum() > 0 ? (Float.valueOf(String.format("%.2f", deviceDataVO.getInactiveNum() * 100.0 / totalPileNum)) + "%") : "0%");
        deviceDataVO.setForbiddenRate(totalPileNum > 0 && deviceDataVO.getForbiddenNum() > 0 ? (Float.valueOf(String.format("%.2f", deviceDataVO.getForbiddenNum() * 100.0 / totalPileNum)) + "%") : "0%");

        screenDataVO.setUserCountList(userCountList);
        screenDataVO.setOrderCountList(orderCountList);
        screenDataVO.setRechargeCountList(rechargeCountList);

        screenDataVO.setTotalUser(totalUser);
        screenDataVO.setTotalConsume(totalConsume);
        screenDataVO.setTotalOrders(totalOrders);
        screenDataVO.setTotalRecharge(totalRecharge);

        screenDataVO.setTotalMerchantNum(totalMerchantNum);
        screenDataVO.setTotalStationNum(totalStationNum);
        screenDataVO.setTotalPileNum(totalPileNum);

        screenDataVO.setDeviceCountData(deviceDataVO);

        //多少路设备
        //screenDataVO.setGunCountList(gunCountList);

        Integer totalDeviceType = 0;
        for(ScreenDeviceTypeVO screenDeviceTypeVO : deviceTypeList){
            totalDeviceType += screenDeviceTypeVO.getPileNum();
        }

        Map deviceType = new Hashtable();
        deviceType.put("EB","单车桩");
        deviceType.put("PB","充电宝");
        deviceType.put("EBox","充电柜");
        deviceType.put("EV","汽车桩");
        for(ScreenDeviceTypeVO screenDeviceTypeVO : deviceTypeList){
            screenDeviceTypeVO.setRate(Float.valueOf(String.format("%.2f", screenDeviceTypeVO.getPileNum() * 100.0 / totalDeviceType)) + "%");
            screenDeviceTypeVO.setDeviceTypeName(deviceType.get(screenDeviceTypeVO.getDeviceType())+"");
        }
        screenDataVO.setDeviceTypeList(deviceTypeList);

        screenDataVO.setRechargeNoticeList(rechargeNoticeList);
        screenDataVO.setStationOrderRankList(stationOrderRankList);
        screenDataVO.setAreaUserRankList(areaUserRankList);
        screenDataVO.setOrderNoticeList(orderNoticeList);
        screenDataVO.setOfflineNoticeList(offlineNoticeList);

        screenDataVO.setStationProvinceList(stationProvinceList);

        return screenDataVO;
    }

    private void fillList(List<ScreenDataItemVO> userCountList, List<ScreenDataItemVO> orderCountList, List<ScreenDataItemVO> rechargeCountList, Date nowTime) {
        for (int index = 1; index <= 7; index++) {
            DateTime dayTime = DateUtil.offsetDay(nowTime, index - 7);
            String dayStr = DateUtil.format(dayTime, "MM.dd");

            boolean userFind = false;
            for (ScreenDataItemVO dataItemVO : userCountList) {
                if (dayStr.equals(StringUtil.getTrim(dataItemVO.getDay()))) {
                    userFind = true;
                    break;
                }
            }
            if (!userFind) {
                ScreenDataItemVO addItemVO = new ScreenDataItemVO();
                addItemVO.setDay(dayStr);
                addItemVO.setNum(0);
                addItemVO.setValue(0);
                userCountList.add(index - 1, addItemVO);
            }

            boolean orderFind = false;
            for (ScreenDataItemVO dataItemVO : orderCountList) {
                if (dayStr.equals(StringUtil.getTrim(dataItemVO.getDay()))) {
                    orderFind = true;
                    break;
                }
            }

            if (!orderFind) {
                ScreenDataItemVO addItemVO = new ScreenDataItemVO();
                addItemVO.setDay(dayStr);
                addItemVO.setNum(0);
                addItemVO.setValue(0);
                orderCountList.add(index - 1, addItemVO);
            }

            boolean rechargeFind = false;
            for (ScreenDataItemVO dataItemVO : rechargeCountList) {
                if (dayStr.equals(StringUtil.getTrim(dataItemVO.getDay()))) {
                    rechargeFind = true;
                    break;
                }
            }

            if (!rechargeFind) {
                ScreenDataItemVO addItemVO = new ScreenDataItemVO();
                addItemVO.setDay(dayStr);
                addItemVO.setNum(0);
                addItemVO.setValue(0);
                rechargeCountList.add(index - 1, addItemVO);
            }

        }
    }

}
